﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;
using Common.Utilities;
using Common.Utilities.Dispatcher;

namespace Workflows.Components.ETL
{
    /// <summary>
    /// 
    /// </summary>
    [Serializable]
    public class DataCleaningOutput:ResultBase
    {
        #region props
        private Dictionary<Guid, int> _RuleIDsApplied;
        /// <summary></summary>
        public Dictionary<Guid, int> RuleIDsApplied
        {
            get
            {
                return this._RuleIDsApplied;
            }
            set
            {
                this._RuleIDsApplied = value;
            }
        }

        private int _RecordsValidated;
        /// <summary></summary>
        public int RecordsValidated
        {
            get
            {
                return this._RecordsValidated;
            }
            set
            {
                this._RecordsValidated = value;
            }
        }

        private int _RecordsFailed;
        /// <summary></summary>
        public int RecordsFailed
        {
            get
            {
                return this._RecordsFailed;
            }
            set
            {
                this._RecordsFailed = value;
            }
        }
        #endregion

        #region ctor
        ///<summary>
        ///</summary>
        public DataCleaningOutput():base(Guid.Empty,Guid.Empty)
        {
            this._RuleIDsApplied = null;
            this._RecordsValidated = 0;
            this._RecordsFailed = 0;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="componentID"></param>
        /// <param name="jobTicketID"></param>
        /// <param name="ruleIDsApplied"></param>
        /// <param name="recordValidated"></param>
        /// <param name="recordNotValidated"></param>
        public DataCleaningOutput(Guid componentID, Guid jobTicketID,
            Dictionary<Guid,int> ruleIDsApplied,
            int recordValidated,
            int recordNotValidated) 
            : base(componentID, jobTicketID)
        {
            this._RuleIDsApplied = ruleIDsApplied;
            this._RecordsFailed = recordNotValidated;
            this._RecordsValidated = recordValidated;
        }
        #endregion

        ///<summary>
        ///</summary>
        ///<param name="xDoc"></param>
        ///<param name="parentNode"></param>
        ///<returns></returns>
        public override XmlNode Serialize(ref XmlDocument xDoc, ref XmlNode parentNode)
        {
            XmlNode xNode = XmlDataUtil.AddElement(ref xDoc, parentNode, "Output");
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "Type", this.GetType().FullName);
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "WorkerID", this.OwnerWorkerInstanceID.ToString());
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "FinishTime", this.FinishTime.ToString());

            XmlDataUtil.UpdateAttribute(ref xDoc, xNode,"RecordsValidated",this.RecordsValidated.ToString());
            XmlDataUtil.UpdateAttribute(ref xDoc,xNode,"RecordsNotValidated",this.RecordsFailed.ToString());
            if(this.RuleIDsApplied !=null && this.RuleIDsApplied.Count>0)
            {
                XmlNode ruleIDsStatsRootNode = XmlDataUtil.AddElement(ref xDoc, xNode, "RulesApplied");
                foreach(Guid ruleID in this.RuleIDsApplied.Keys)
                {
                    XmlNode ruleNode = XmlDataUtil.AddElement(ref xDoc, ruleIDsStatsRootNode, "Rule");
                    XmlDataUtil.UpdateAttribute(ref xDoc,ruleNode,"RuleID",ruleID.ToString());
                    XmlDataUtil.UpdateAttribute(ref xDoc,ruleNode,"Count",this.RuleIDsApplied[ruleID].ToString());
                }
            }

            return xNode;
        }

        ///<summary>
        ///</summary>
        ///<param name="xNode"></param>
        ///<returns></returns>
        ///<exception cref="Exception"></exception>
        public override ResultBase Instantiate(XmlNode xNode)
        {
            if (xNode.Attributes.GetNamedItem("Type").Value == this.GetType().FullName)
            {
                DataCleaningOutput output = new DataCleaningOutput();
                // output.OwnerComponentID = new Guid(xNode.Attributes.GetNamedItem("ComponentID").Value);
                output.OwnerWorkerInstanceID = new Guid(xNode.Attributes.GetNamedItem("WorkerID").Value);
                output.FinishTime = DateTime.Parse(xNode.Attributes.GetNamedItem("FinishTime").Value);

                output.RuleIDsApplied = null;
                XmlNodeList ruleNodes = xNode.SelectNodes("//Rule");
                if(ruleNodes !=null && ruleNodes.Count>0)
                {
                    foreach(XmlNode ruleNode in ruleNodes)
                    {
                        Guid ruleID=new Guid(XmlDataUtil.GetAttributeValue(ruleNode,"RuleID",Guid.Empty.ToString()));
                        int count = int.Parse(XmlDataUtil.GetAttributeValue(ruleNode, "Count", "0"));
                        output.RuleIDsApplied.Add(ruleID,count);
                    }
                }
                output.RecordsValidated = int.Parse(XmlDataUtil.GetAttributeValue(xNode, "RecordsValidated", "0"));
                output.RecordsFailed = int.Parse(XmlDataUtil.GetAttributeValue(xNode, "RecordsNotValidated", "0"));
                
                return output;
            }
            else
            {
                throw new Exception("Unable to instantiate output: invalid type specified in xml node");
            }
        }
    }
}
